package co.id.vivastor.pantau;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import co.id.vivastor.pantau.util.Global;
import co.id.vivastor.pantau.util.SMSSender;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;

public class KeamananActivity extends Activity {

	public static final String IDENTIFIER = "KEAMANAN";
	private int _selectedPotensiKejadian;
	private String _tanggalLaporan;
	private String _keterangan;
	
	DateFormat fmtDateAndTime = new SimpleDateFormat("dd-MMM-yyyy"); 
	DateFormat fmtDateAndTime2 = new SimpleDateFormat("dd/MM/yyyy"); 
	//Date Label
	TextView dateLabel; 
	//DatePicker Dialog:
	Calendar dateAndTime = Calendar.getInstance();
	DatePickerDialog.OnDateSetListener datePicker = new DatePickerDialog.OnDateSetListener() {
	    public void onDateSet(DatePicker view, int year, int monthOfYear,
	                int dayOfMonth) {
	      dateAndTime.set(Calendar.YEAR, year);
	      dateAndTime.set(Calendar.MONTH, monthOfYear);
	      dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
	      dateLabel.setText(fmtDateAndTime.format(dateAndTime.getTime()));
	      KeamananActivity.this._tanggalLaporan = fmtDateAndTime2.format(dateAndTime.getTime()).toString();
	      Toast.makeText(getApplicationContext(), fmtDateAndTime.format(dateAndTime.getTime()), Toast.LENGTH_SHORT).show();
	    }
	  };  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_keamanan);
		_setOtherViews();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_keamanan, menu);
		return true;
	}
	
	private void _setOtherViews() {
		//Icon Clickable goto home menu:
		ImageView icon = (ImageView) findViewById(R.id.imageView1);
		Global.setImageViewButtonListener(icon, KeamananActivity.this, MainActivity.class);
		
		//Set Spinner Potensi Kejadian
		Spinner spinnerPotensiKejadian = (Spinner) findViewById(R.id.spinner_potensi_kejadian);
		ArrayAdapter<CharSequence> adapterPotensiKejadian = ArrayAdapter.createFromResource(this, R.array.keamanan_potensi_kejadian, R.layout.spinner_layout);
		adapterPotensiKejadian.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerPotensiKejadian.setAdapter(adapterPotensiKejadian);
		spinnerPotensiKejadian.setOnItemSelectedListener(
				new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						KeamananActivity.this._selectedPotensiKejadian = (int)arg0.getItemIdAtPosition(arg2);
					}
					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
					}
				}
		);
		
		//Set Listener Pick Date:
		Button buttonPickDate = (Button) findViewById(R.id.buttonPickDate);
	    
	    buttonPickDate.setOnClickListener(new View.OnClickListener() {
	      public void onClick(View v) {
	        new DatePickerDialog(KeamananActivity.this,
	            datePicker,
	            dateAndTime.get(Calendar.YEAR),
	            dateAndTime.get(Calendar.MONTH),
	            dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
	      }
	    });
	    
	    dateLabel = (TextView) findViewById(R.id.textView_pickedDate);
	    dateLabel.setText(fmtDateAndTime.format(dateAndTime.getTime()));
	    KeamananActivity.this._tanggalLaporan = fmtDateAndTime2.format(dateAndTime.getTime()).toString();
        
        //Set Listener Send Button
    	Button btnSendSMS = (Button) findViewById(R.id.buttonSend);
		btnSendSMS.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Cek setting udah terisi:
				String kodeWilayah = Global.getData(getApplicationContext(), Global.KODE_WILAYAH, Global.DEFAULT_VALUE);
				String nomorTPS = Global.getData(getApplicationContext(), Global.NOMOR_TPS, Global.DEFAULT_VALUE);
				String SMSCenterNo = Global.getData(getApplicationContext(), Global.SMS_CENTER_NO, Global.DEFAULT_VALUE);
				
				if (kodeWilayah == Global.DEFAULT_VALUE || nomorTPS == Global.DEFAULT_VALUE || SMSCenterNo == Global.DEFAULT_VALUE ||
						kodeWilayah == "" || nomorTPS == "" || SMSCenterNo == "") {
					//Toast.makeText(getApplicationContext(), "Maaf, Setting belum terkonfigurasi", Toast.LENGTH_SHORT).show();
					Global.showDialog(KeamananActivity.this, "Konfigurasi", "Maaf, setting belum terkonfigurasi. Cek halaman Setting.", "Setting");
				} else {
					EditText  keterangan = (EditText) findViewById(R.id.editText_keterangan);
					KeamananActivity.this._keterangan = keterangan.getText().toString();
					
					SMSSender smsSender = new SMSSender(KeamananActivity.this);
					String message = KeamananActivity.IDENTIFIER+" "+kodeWilayah+" "+nomorTPS+" "+
									KeamananActivity.this._selectedPotensiKejadian+" "+KeamananActivity.this._tanggalLaporan+" "+KeamananActivity.this._keterangan;
					smsSender.sendSMS(SMSCenterNo, message);
				}
			}
		});
		
	}
}
